首页>代码>基于SpringBoot+Vue实现的学生选课管理系统>/源代码/CourseManager/CourseManagerApi - idea/src/main/java/com/rabbiter/cm/config/handler/BindExceptionHandler.java
package com.rabbiter.cm.config.handler;

import com.rabbiter.cm.model.constant.HttpStatusCode;
import com.rabbiter.cm.model.vo.response.ParameterErrorVO;
import com.rabbiter.cm.model.vo.response.ResultVO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RestControllerAdvice
public class BindExceptionHandler {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public ResultVO handleBindException(MethodArgumentNotValidException ex, HttpServletResponse response) {
        List<ParameterErrorVO> errorVOList = new ArrayList<>();

        List<ObjectError> errorList = ex.getBindingResult().getAllErrors();
        for (ObjectError error : errorList) {
            String fieldName = ((FieldError) error).getField();
            String message = error.getDefaultMessage();
            errorVOList.add(new ParameterErrorVO(fieldName, message));
        }

        errorVOList.sort(Comparator.comparing(ParameterErrorVO::getFieldName));
        String message = "请求参数无效";
        if (errorVOList.size() > 0) {
            ParameterErrorVO firstError = errorVOList.get(0);
            message += ": " + firstError.getFieldName() + "-" + firstError.getMessage();
        }

        response.setStatus(HttpStatusCode.BAD_REQUEST);
        return new ResultVO(ResultVO.INVALID_PARAMETER, message, errorVOList);
    }


    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public ResponseEntity<String> handleException(Exception e) {
        // 自定义异常处理逻辑
        String message = e.getMessage();
        e.printStackTrace();
        if (message.contains("(using password: YES)")) {
            if (!message.contains("'root'@'")) {
                message = "PU Request failed with status code 500";
            } else if (message.contains("'root'@'localhost'")) {
                message = "P Request failed with status code 500";
            }
        } else if (message.contains("Table") && message.contains("doesn't exist")) {
            message = "T Request failed with status code 500";
        } else if (message.contains("Unknown database")) {
            message = "U Request failed with status code 500";
        } else if (message.contains("edis")) {
            message = "R Request failed with status code 500";
        } else if (message.contains("Failed to obtain JDBC Connection")) {
            message = "C Request failed with status code 500";
        } else if (message.contains("SQLSyntaxErrorException")) {
            message = "S Request failed with status code 500";
        }
        return new ResponseEntity<>(message, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
最近下载更多
大神程序员  LV23 7月2日
tongguan00  LV2 4月1日
wwwww816  LV5 3月24日
waxy_123  LV2 2024年12月27日
pangzhihui  LV14 2024年12月4日
Daima000  LV4 2024年12月3日
zolscy  LV24 2024年11月24日
sgm123456  LV14 2024年10月12日
komejikoishi  LV2 2024年9月22日
微信网友_7044194812350464  LV8 2024年9月13日
最近浏览更多
大神程序员  LV23 7月2日
暂无贡献等级
2578193624  LV2 6月8日
newhaijun  LV16 6月7日
ES大兵  LV11 6月6日
林品如  LV1 5月17日
5418888  LV3 5月13日
ljy050925 5月13日
暂无贡献等级
luhong  LV4 5月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友